/*!
 * @文件名 logic_mode_eol.c
 *
 * @功能描述
 *	开机一定时间内,如果检测到上位机发来的命令,则进入EOL模式,方便进行下线配置
 */
#include "logic_public.h"

#define READ_COMD	0x22
#define WRITE_COMD	0x2E


void fml_eol_mode(uint8_t *pBuf);

uint8_t test_slaveRxBuffer[8];
uint8_t test_slaveTxBuffer[6];
static bool g_bEolMode = false;	//false[有主机的模式] true[无主机的EOL产测模式]

extern ST_VENDOR_DATA gst_vendor_A_data;

void (*p_test_commod)(uint8_t*) = fml_eol_mode;
static task_t s_eol_task;
static task_t s_vendor_task;

extern void logic_parse_init(void);
extern void fml_ds90ub948_read_commod(uint8_t *pBuf);
extern uint8_t pub_slaveRxBuffer[2];
extern uint8_t pub_slaveTxBuffer[1];
extern uint8_t	result1,result2;

void (*p_turn_read)(uint8_t*) = fml_ds90ub948_read_commod;
//extern static task_t s_eol_task;

void logic_vendor_task(void *arg, uint32_t event)
{
	(void)arg;
	if(event == 1)
	{
		printf("vendor_data_load");
		vendor_data_load();
	}
	else if(event == 2)
	{
		printf("vendor_data_save");
		vendor_data_save();
	}
}


void check_eol_mode_exist(void *arg, uint32_t event)
{
	static int iCnt = 10;
	(void)arg;
	if(event == 1)
	{//接收底层发来的事件--948 Write
		cola_delay_ms(5);//这个延时必须要,否则切换命令后仍是上一个,原因待查!
		fml_eol_mode(test_slaveRxBuffer);
	}
	if(iCnt > 0)
	{
		iCnt--;
		if(iCnt == 0)
		{
			if(g_bEolMode == false)
			{
				cola_timer_stop(&s_eol_task);
				fml_ds90ub948_close();
				i2c_pal_lpi2c0_SlaveConfig0.slaveAddress = 0x6B;
				logic_parse_init();
				logWrite("logic_parse_init\r\n");
			}
			else
			{
				logWrite("enter eol mode\r\n");
			}
		}
	}
}

#if 0
void eol_mode_var_init(void)
{
	gst_vendor_A_data.SOFTWARE_VERSION0 = 'c';
	gst_vendor_A_data.SOFTWARE_VERSION1 = 'u';
	gst_vendor_A_data.SOFTWARE_VERSION2 = 's';
	gst_vendor_A_data.SOFTWARE_VERSION3 = 's';
	gst_vendor_A_data.SOFTWARE_VERSION4 = 'w';

	gst_vendor_A_data.HARDWARE_VERSION0 = 'c';
	gst_vendor_A_data.HARDWARE_VERSION1 = 'u';
	gst_vendor_A_data.HARDWARE_VERSION2 = 's';
	gst_vendor_A_data.HARDWARE_VERSION3 = 'h';
	gst_vendor_A_data.HARDWARE_VERSION4 = 'w';

	gst_vendor_A_data.PN_VERSION0 = 'p';
	gst_vendor_A_data.PN_VERSION1 = 'n';
	gst_vendor_A_data.PN_VERSION2 = 'v';
	gst_vendor_A_data.PN_VERSION3 = 'e';
	gst_vendor_A_data.PN_VERSION4 = 'r';


	gst_vendor_A_data.TFT_SW_VERSION0= 't';
	gst_vendor_A_data.TFT_SW_VERSION1= 'f';
	gst_vendor_A_data.TFT_SW_VERSION2= 't';
	gst_vendor_A_data.TFT_SW_VERSION3= 's';
	gst_vendor_A_data.TFT_SW_VERSION4= 'w';

	gst_vendor_A_data.TFT_HW_VERSION0 = 't';
	gst_vendor_A_data.TFT_HW_VERSION1 = 'f';
	gst_vendor_A_data.TFT_HW_VERSION2 = 't';
	gst_vendor_A_data.TFT_HW_VERSION3 = 'h';
	gst_vendor_A_data.TFT_HW_VERSION4 = 'w';

	gst_vendor_A_data.DH_SW_VERSION0 = 'd';;
	gst_vendor_A_data.DH_SW_VERSION1 = 'h';;
	gst_vendor_A_data.DH_SW_VERSION2 = 's';;
	gst_vendor_A_data.DH_SW_VERSION3 = 'w';;
	gst_vendor_A_data.DH_SW_VERSION4 = '0';;

	gst_vendor_A_data.DH_HW_VERSION0 = 'd';
	gst_vendor_A_data.DH_HW_VERSION1 = 'h';
	gst_vendor_A_data.DH_HW_VERSION2 = 'h';
	gst_vendor_A_data.DH_HW_VERSION3 = 'w';
	gst_vendor_A_data.DH_HW_VERSION4 = '0';
}
#endif

void eol_mode_open(void)
{
	int test_Rx_length = sizeof(test_slaveRxBuffer);
	int test_Tx_length = sizeof(test_slaveTxBuffer);
	//eol_mode_var_init();
#if 0
	fml_ds90ub948_init(test_slaveRxBuffer,test_Rx_length,test_slaveTxBuffer,test_Tx_length,p_test_commod,NULL);
	cola_timer_create(&s_eol_task, check_eol_mode_exist, NULL);
	cola_timer_start(&s_eol_task, EN_COLA_TIMER_ALWAYS, 10);
	cola_task_create(&s_vendor_task, logic_vendor_task, NULL);
	//vendor_factory_set();
#else
	fml_ds90ub948_init(test_slaveRxBuffer,test_Rx_length,test_slaveTxBuffer,test_Tx_length,NULL,&s_eol_task);
	cola_task_create(&s_eol_task,check_eol_mode_exist,NULL);
#endif
}

void fml_eol_mode(uint8_t *pBuf)
{
	bool bEnVendorSave = false;
	switch (pBuf[1])
	{
	        case EOL_CUSTOMER_SW_VERSION:
	        	g_bEolMode = true;
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.SOFTWARE_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.SOFTWARE_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.SOFTWARE_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.SOFTWARE_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.SOFTWARE_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_A_data.SOFTWARE_VERSION5;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.SOFTWARE_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.SOFTWARE_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.SOFTWARE_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.SOFTWARE_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.SOFTWARE_VERSION4 = pBuf[6];
	        		gst_vendor_A_data.SOFTWARE_VERSION5 = pBuf[7];
	        		bEnVendorSave = true;
	        	}
	            break;
	        case EOL_PN_VERSION:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.PN_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.PN_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.PN_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.PN_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.PN_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_A_data.PN_VERSION5;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.PN_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.PN_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.PN_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.PN_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.PN_VERSION4 = pBuf[6];
	        		gst_vendor_A_data.PN_VERSION5 = pBuf[7];
	        		bEnVendorSave = true;
	        	}
	       	    break;
	        case EOL_CUSTOMER_HW_VERSION:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.HARDWARE_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.HARDWARE_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.HARDWARE_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.HARDWARE_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.HARDWARE_VERSION4;
	        		test_slaveTxBuffer[5] = gst_vendor_A_data.HARDWARE_VERSION5;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.HARDWARE_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.HARDWARE_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.HARDWARE_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.HARDWARE_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.HARDWARE_VERSION4 = pBuf[6];
	        		gst_vendor_A_data.HARDWARE_VERSION5 = pBuf[7];
	        		bEnVendorSave = true;
	        	}
	       	    break;
	        case EOL_PANNL_HARDWARE:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.TFT_HW_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.TFT_HW_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.TFT_HW_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.TFT_HW_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.TFT_HW_VERSION4;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.TFT_HW_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.TFT_HW_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.TFT_HW_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.TFT_HW_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.TFT_HW_VERSION4 = pBuf[6];
	        		bEnVendorSave = true;
	        	}
	       	     break;
	        case EOL_PANNL_SOFTWAER:
				if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.TFT_SW_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.TFT_SW_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.TFT_SW_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.TFT_SW_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.TFT_SW_VERSION4;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.TFT_SW_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.TFT_SW_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.TFT_SW_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.TFT_SW_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.TFT_SW_VERSION4 = pBuf[6];
	        		bEnVendorSave = true;
	        	}
	       	    break;
	        case EOL_BLACKLIGHT_EN:
	        	if (pBuf[0] == WRITE_COMD)
	        	{
	    			fml_max25512_set_bklight_on(pBuf[2]);
	        	}
	        	else if (pBuf[0] == READ_COMD)
				{
	        		test_slaveTxBuffer[0] = fml_max25512_get_bklight_state()?1:0;
				}
	       	     break;
	        case EOL_BLACKLIGHT_DUTY:
	        	if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] =  fml_get_host_pwm_value();
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		fml_set_host_pwm_value(pBuf[2]);
	            	fml_pwm_change_duty(pBuf[2]);
	        	}
	       	     break;
	        case EOL_DIAGNOSTIC_1:
	        	test_slaveTxBuffer[0] =  result1;
	       	     break;
	        case EOL_DIAGNOSTIC_2:
	        	test_slaveTxBuffer[0] =  result2;
	       	     break;
	        case EOL_TEMP_AD_VALUE:
	        	test_slaveTxBuffer[0] = fml_adc_convert_to_temperature(fml_get_tft_ntc_adc());
	       	     break;
	        case EOL_DH_SW_VERSION:
	        	 if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.DH_SW_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.DH_SW_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.DH_SW_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.DH_SW_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.DH_SW_VERSION4;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.DH_SW_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.DH_SW_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.DH_SW_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.DH_SW_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.DH_SW_VERSION4 = pBuf[6];
	        		bEnVendorSave = true;
	        	}
	        	 break;
	        case EOL_DH_HW_VERSION:
	        	 if(pBuf[0] == READ_COMD)
	        	{
	        		test_slaveTxBuffer[0] = gst_vendor_A_data.DH_HW_VERSION0;
	        		test_slaveTxBuffer[1] = gst_vendor_A_data.DH_HW_VERSION1;
	        		test_slaveTxBuffer[2] = gst_vendor_A_data.DH_HW_VERSION2;
	        		test_slaveTxBuffer[3] = gst_vendor_A_data.DH_HW_VERSION3;
	        		test_slaveTxBuffer[4] = gst_vendor_A_data.DH_HW_VERSION4;
	        	}
	        	else if (pBuf[0] == WRITE_COMD)
	        	{
	        		gst_vendor_A_data.DH_HW_VERSION0 = pBuf[2];
	        		gst_vendor_A_data.DH_HW_VERSION1 = pBuf[3];
	        		gst_vendor_A_data.DH_HW_VERSION2 = pBuf[4];
	        		gst_vendor_A_data.DH_HW_VERSION3 = pBuf[5];
	        		gst_vendor_A_data.DH_HW_VERSION4 = pBuf[6];
	        		bEnVendorSave = true;
	        	}
	        	 break;
	        case EOL_TFT_COLOR_SET:
	        	fml_gpio_write(OUT_TFT_BIST,(pBuf[2] > 0)?true:false);
	    		fml_tft_BIST_clear();
	    		switch(pBuf[2])
	    		{
	    		case 0x01:
	    			fml_tft_write_BIST_mode(PTGN_RED);
	    			break;
	    		case 0x02:
	    			fml_tft_write_BIST_mode(PTGN_GREEN);
	    			break;
	    		case 0x03:
	    			fml_tft_write_BIST_mode(PTGN_BLUE);
	    			break;
	    		case 0x04:
	    			fml_tft_write_BIST_mode(PTGN_WHITE);
	    			break;
	    		case 0x05:
	    			fml_tft_write_BIST_mode(PTGN_BLACK);
	    			break;
	    		case 0x06:
	    			fml_tft_write_BIST_mode(PTGN_H_GRAY_A);
	    			break;
	    		case 0x07:
					fml_tft_write_BIST_mode(PTGN_CBAR_A);
					break;
				case 0x08:
					fml_tft_write_BIST_mode(PTGN_CHESS_A);
					break;
	    		default:
	    			break;
	    		}
	        	 break;
	        default:
	             break;
	 }
	 if(bEnVendorSave)
	 	vendor_data_save();
}

